<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>子父组件传值</title>
    <style>
      li {
        cursor: pointer;
        margin-bottom: 10px;
      }
      .li-select {
        color: tomato;
      }
    </style>
    <script src="../lib/vue_v2.6.14.js"></script>
  </head>
  <body>
    <div id="app">
      <cpn-son @change="change"></cpn-son>
    </div>

    <template id="cpn">
      <div>
        <ul>
          <li v-for="(item, index) in list" :key="index" :class="{'li-select': currentIndex === index }" @click="itemClick(item, index)">{{ item.name }}</li>
        </ul>
      </div>
    </template>
    <script>
      // 子组件
      Vue.component('cpn-son', {
        template: '#cpn',
        data() {
          return {
            list: [
              { id: 1, name: '张三' },
              { id: 2, name: '李四' },
              { id: 3, name: '王五' },
              { id: 4, name: '赵六' }
            ],
            currentIndex: ''
          }
        },
        methods: {
          itemClick(item, index) {
            this.currentIndex = index

            // 自定义事件
            this.$emit('change', item)
          }
        }
      })

      // 父组件
      const vm = new Vue({
        el: '#app',
        data: {},
        methods: {
          change(item) {
            console.log('子组件传过来的：', item.name)
          }
        }
      })
    </script>
  </body>
</html>
